<?php
class UserAction extends CommonAction {
    public function index() {
        $this->display();
    }

    //注册
    public function reg() {
        if(cookie('user')) $this->redirect('/');
        import('UserModel', './Admin/Lib/Model/');
        $user = new UserModel();
        if(isset($_POST['send'])) {
            if($user->create()) {
                if($user->add()) {
                    //如果本次注册是推广者的功劳
                    if(isset($_COOKIE['proid'])) {
                        $user->where('id='.$_COOKIE['proid'])->setInc('score', 10);
                        setcookie('proid', '', time()-1, '/');            //注销本次推广cookie
                    }
                    $this->success('注册成功', U('User/login'));
                } else {
                    $this->error('系统出错，注册失败!', 0);
                }
            } else {
                $this->error($user->getError(), 0);
            }
        }
        $this->display();
    }

    //登陆
    public function login() {
        if(cookie('user')) $this->redirect('/');
        if(isset($_POST['send'])) {
            $user = D('User');
            $name = $this->_post('name');
            $pass = sha1($this->_post('pass'));
            if(empty($name) || empty($pass)) {
                $this->error('用户名和密码必须填写!', 0);
            }
            if($this->_post('checkcode') !== session('code')) $this->error('验证码错误!', 0);
            if(!$user->where("name='$name'")->getField('id')) {
                $this->error('用户名不存在!', 0);
            } else {
                if(!$user->where("pass='$pass'")->getField('id')) $this->error('用户密码错误!', 0);
            }
            if($_POST['remember'] == 1) {
                setcookie('user', $name, time()+60*60*24*7, '/');
            } else {
                setcookie('user', $name, -1, '/');       //-1表示会话结束时销毁
            }
            $data['last_time'] = createDate();
            $data['last_ip'] = getIp();
            $user->where("name='$name'")->save($data);
            $this->success('登录成功', '__ROOT__');
        }
        $this->display();
    }

    //找回密码
    public function findpass() {
        C('TOKEN_ON',false);
        if(cookie('user')) $this->redirect('__ROOT__');
        if(isset($_POST['send'])) {
            $user = D('User');
            if($this->_post('checkcode') != session('code')) $this->error('验证码错误!', 0);
            $name = $this->_post('name');
            $pass = $this->_post('pass');
            $notpass = $this->_post('notpass');
            $condition['name'] = $name;
            $condition['email'] = $this->_post('email');
            if(!$user->where($condition)->find()) $this->error('用户名和邮箱对应不正确!');
            $info = $user->field('ques,ans')->where("name='$name'")->find();
            $ques = $this->_post('ques');
            $ans = $this->_post('ans');
            if($ques != $info['ques']) $this->error('问题选择与注册时不一致!', 0);
            if($ques == $info['ques'] && $ans != $info['ans']) $this->error('问题回答错误!', 0);
            if(mb_strlen($pass, 'utf-8') < 6) $this->error('密码长度不能低于六位!', 0);
            if($notpass != $pass) $this->error('两次密码输入不一致!', 0);
            $user->where("name='$name'")->setField('pass', sha1($pass)) ? $this->success('密码成功修改,请注意保管', U('User/login')) : $this->error('系统出错，密码修改失败!', 0);
        }
        $this->display();
    }

    //退出
    public function logout() {
        if(setcookie('user', '', time()-1, '/')) $this->redirect('/User/login');

    }

    //验证用户名唯一性
    public function uniqueName() {
        $user = D('User');
        echo $user->where("name='{$_POST['name']}'")->find() ? 1 : 0;
    }

    //验证邮件唯一性
    public function uniqueEmail() {
        $user = D('User');
        echo $user->where("email='{$_POST['email']}'")->find() ? 1 : 0;
    }

    //ajax检查验证码
    public function ajaxCheckCode() {
        echo $_POST['code'] == session('code') ? 1 : 0;
    }

    //ajax登陆
    public function ajaxLogin() {
        $user = M('User');
        $name = $this->_post('name');
        $pass = sha1($this->_post('pass'));
        $code = $this->_post('code');
        $remeber = $this->_post('remeber');
        if($_POST['code'] !== session('code')) {
            echo 'code 404';
        } elseif(!$user->field('id')->where("name='$name'")->find()) {
            echo 'user 404';
        } else if(!$user->field('id')->where("pass='$pass'")->find()) {
            echo 'pass 404';
        } else {
            if($remeber == 1) {
                setcookie('user', $name, time()+60*60*24*7, '/');
            } else {
                setcookie('user', $name, -1, '/');
            }
            echo 'ok';
        }

    }


}

?>